/**
 * @file app_main.c
 * @author  xiaowine (xiaowine@sina.cn)
 * @brief
 * @version 01.00
 * @date    2022-03-02
 *
 * @copyright Copyright (c) {2020}  xiaowine
 *
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2022-03-02 <td>1.0     <td>wangh     <td>内容
 * </table>
 * ******************************************************************
 * *                   .::::
 * *                 .::::::::
 * *                ::::::::::
 * *             ..:::::::::::
 * *          '::::::::::::
 * *            .:::::::::
 * *       '::::::::::::::..        女神助攻,流量冲天
 * *            ..::::::::::::.     永不宕机,代码无bug
 * *          ``:::::::::::::::
 * *           ::::``:::::::::'        .:::
 * *          ::::'   ':::::'       .::::::::
 * *        .::::'      ::::     .:::::::'::::
 * *       .:::'       :::::  .:::::::::' ':::::
 * *      .::'        :::::.:::::::::'      ':::::
 * *     .::'         ::::::::::::::'         ``::::
 * * ...:::           ::::::::::::'              ``::
 * *```` ':.          ':::::::::'                  ::::.
 * *                   '.:::::'                    ':'````.
 * ******************************************************************
 */

/* Private includes ----------------------------------------------------------*/
#include "RTE_Components.h"
#include CMSIS_device_header
#include <stdio.h>
#include <string.h>
#include "app_main.h"
#include "cmsis_os2.h"
#include "iwdg.h"
#include "usart.h"
#include "user_data.h"
/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private user code ---------------------------------------------------------*/
/**
 * @brief  userAPP
 */

static const osThreadAttr_t ThreadAttr_start_main = {"app_main", NULL, NULL, NULL, NULL, NULL, osPriorityNormal, NULL, NULL};
osThreadId_t                userTid;  // thread id
static const osThreadAttr_t ThreadAttr_USERAPP = {"userAPP", NULL, NULL, NULL, NULL, NULL, osPriorityNormal, NULL, NULL};
osThreadId_t                bleTid;  // thread id
static const osThreadAttr_t ThreadAttr_bleAPP = {"bleAPP", NULL, NULL, NULL, NULL, NULL, osPriorityNormal, NULL, NULL};
osThreadId_t                I2cTid;  // thread id
static const osThreadAttr_t ThreadAttr_i2c1APP = {"i2c1APP", NULL, NULL, NULL, NULL, NULL, osPriorityNormal, NULL, NULL};

/***********************************************************************/
_TKS_FLAGA_type    TKS_FLAGA;
static void        timerCall(void *arg);  // prototype for timer callback function
static osTimerId_t timer63Ms_handle;      // timer id
static osTimerId_t timer250Ms_handle;     // timer id
static osTimerId_t timer500Ms_handle;     // timer id

static const osTimerAttr_t timerAttr_timer63 = {
    .name = "timer63Ms",
};
static const osTimerAttr_t timerAttr_timer250 = {
    .name = "timer250Ms",
};
static const osTimerAttr_t timerAttr_timer500 = {
    .name = "timer500Ms",
};
/*----------------------------------------------------------------------------
 * Application main thread
 *---------------------------------------------------------------------------*/
static void timerCall(void *param)
{
    switch ((uint32_t)param) {
        case 63:
            TKS_63MSF = 1;
            break;
        case 250:
            TKS_250MSF = 1;
            break;
        case 500:
            TKS_500MSF = 1;
            break;
        default:
            break;
    }
}

__NO_RETURN static void start_main(void *argument)
{
    (void)argument;
    printf("creat timers\r\n");
    timer63Ms_handle  = osTimerNew(&timerCall, osTimerPeriodic, (void *)63, &timerAttr_timer63);
    timer250Ms_handle = osTimerNew(&timerCall, osTimerPeriodic, (void *)250, &timerAttr_timer250);
    timer500Ms_handle = osTimerNew(&timerCall, osTimerPeriodic, (void *)500, &timerAttr_timer500);

    osTimerStart(timer63Ms_handle, 63);
    osTimerStart(timer250Ms_handle, 250);
    osTimerStart(timer500Ms_handle, 500);

    printf("creat thread\r\n");
    userTid = osThreadNew(userProc, NULL, &ThreadAttr_USERAPP);
    bleTid  = osThreadNew(bleProc, NULL, &ThreadAttr_bleAPP);
    I2cTid  = osThreadNew(i2cProc, NULL, &ThreadAttr_i2c1APP);
    // ...
    for (;;) {
        HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
        osDelay(500);
        HAL_IWDG_Refresh(&hiwdg);
    }
}

void app_main(void)
{
    osKernelInitialize();                                   // Initialize CMSIS-RTOS
    osThreadNew(start_main, NULL, &ThreadAttr_start_main);  // Create application main thread
    if (osKernelGetState() == osKernelReady) {
        osKernelStart();  // Start thread execution
    }
}

#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */

#ifdef __cplusplus
extern "C" {
#endif  //__cplusplus

PUTCHAR_PROTOTYPE
{
    // RS485A_DE = DOsnt;
    //发送脚使能，RS485为发送数据状态
    // delay_us(10);
    //等待发送脚的电平稳定
    HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 100);
    //调用STM32的HAL库，发送一个字节
    // delay_us(10);
    //避免数据信号震荡造成回环数据
    // RS485A_DE = DOrec;
    //发送脚除能，RS485恢复到接收数据状态

    return (ch);
}
#ifdef __cplusplus
}
#endif  //__cplusplus
